<template>
  <div>
    <div style="text-align:right;line-height:30px">
      <el-row :gutter="20">
        <el-col :span="16" style="text-align: left">
          <el-button class="tool_button" @click="clickNewButton" type="primary" size="mini" icon="el-icon-plus" plain>新建权限</el-button>
          <el-button class="tool_button" @click="clickDeleteButton" type="danger" size="mini" icon="el-icon-delete" plain>删除权限</el-button>
          <el-button class="tool_button" @click="clickRefreshButton" type="info" size="mini" icon="el-icon-refresh" plain>刷新</el-button>
        </el-col>

        <el-col :span="8">
          <div>
            <el-input placeholder="请输入关键字搜索" v-model="inputText" class="input-with-select" size="mini">
              <el-select v-model="select" slot="prepend" placeholder="请选择">
                <el-option label="全部" value="1"></el-option>
                <el-option label="权限名" value="2"></el-option>
                <el-option label="创建人" value="3"></el-option>
              </el-select>
              <el-button slot="append" icon="el-icon-search" @click="clickSearchButton"></el-button>
            </el-input>
          </div>
        </el-col>

      </el-row>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData.datas"
      tooltip-effect="dark"
      :stripe="true"
      :border="true"
      size="mini"
      style="width: 100%;margin-top:20px"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="序号"
        width="80">
      </el-table-column>
      <el-table-column
        prop="permissionName"
        label="权限名"
        width="200">
      </el-table-column>
      <el-table-column
        prop="permissionCode"
        label="权限编码"
        width="200">
      </el-table-column>
      <el-table-column
        prop="permissionUri"
        label="权限uri"
        width="200">
      </el-table-column>
      <el-table-column
        prop="permissionUrl"
        label="权限路径"
        width="220">
      </el-table-column>
      <el-table-column
        prop="isMenu"
        label="是否菜单"
        width="80">
      </el-table-column>
      <el-table-column
        prop="moduleName"
        label="所属模块"
        width="80">
      </el-table-column>
      <el-table-column
        prop="createUser"
        label="创建人"
        width="130">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column label="操作" style="margin-right:10px;">
        <template slot-scope="scope">
          <icon name="edit" :scale="2" @click.native="handleUpdate(scope.row)" class="pointcss"></icon>
          <i @click="showPermissionOrderDialog(scope.row)"><icon name="order" :scale="2" class="pointcss" ></icon>&nbsp;&nbsp;</i>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top:10px;text-align: center">
      <el-pagination background layout="total,prev, pager, next,jumper" :total="tableData.totals" :current-page="tableData.currentPage" v-on:current-change="pageChange">
      </el-pagination>
    </div>
  </div>
</template>

<script type="text/javascript">
  import { yfuErrorToast } from '@/utils/yfuMessage.js'

  export default {
    data () {
      return {
        /* table begin */
        tableData: {
          datas: [],
          currentPage: 1,
          totals: 160
        },
        multipleSelection: [],
        /* table end */

        /* search begin */
        select: '1',
        inputText: '',
        /* search end */

        /* temp begin */
        temp: {
          id: '',
          permissionName: '',
          permissionUrl: '',
          permissionCode: '',
          permissionUri: '',
          isMenu: '',
          moduleName: '',
          createUser: '',
          createTime: ''
        }
      }
    },
    mounted: function () {
      this.getAllDatas()
    },
    methods: {
      toggleSelection (rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      },
      handleSelectionChange (val) {
        this.multipleSelection = val
      },
      // 获取选中行
      getSelectedRows () {
        return this.multipleSelection
      },
      getAllDatas () {
      },
      pageChange (toPage) {
        if (toPage < 1) {
          toPage = 1
        }
        this.getAllDatas()
      },
      // 新建
      clickNewButton () {
        this.$emit('listenToChildNewButtonEvent')
      },
      // 编辑
      handleUpdate (row) {
        this.temp = Object.assign({}, row) // copy obj
        this.$emit('listenToChildEditButtonEvent', Object.assign({}, row))
      },
      // 删除
      clickDeleteButton () {
        // 获取选中行的数据
        if (this.multipleSelection.length === 0) {
          this.$message('请至少选中一行')
        } else {
          this.$confirm('是否确认删除权限？', '删除权限', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // let row = this.multipleSelection[0]
            console.log('删除成功!')
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        }
      },
      refreshTable () {
        this.pageChange(1)
      },
      clickRefreshButton () {
        this.refreshTable()
      },

      showPermissionOrderDialog (row) {
        this.permissionRow = row
        this.permissionOrderDialog = true
      },
      // 点击搜索按钮
      clickSearchButton () {
        if (this.inputText == null || this.inputText === '') {
          yfuErrorToast('搜索值不能为空')
        } else {
          if (this.select === '1') { // 全部
            console.log('1')
          } else if (this.select === '2') { // 模块名
            console.log('2')
          } else if (this.select === '3') { // 创建人
            console.log('3')
          }
        }
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  .el-select .el-input {
    width: 105px;
  }

</style>
